#ifndef RTDB_H
#define RTDB_H

#include <QtCore>

//前置声明
class CBattStack;
class CBattBank;
class CBAComReg;
class CPcsData;
class CMeterData;
class CFireData;
class CAcData;
class CTransData;
class CAdamIOData;
class CIoData;
class CLcData;
class CEcData;
class CThData;
class CDhData;
class CWaterData;
class CDoorData;
class CDcdcData;
class CCpData;
class CGasData;
class CUpsData;
class CReserve1Data;
class CAppData;
class CLedData;
class CFanData;
class CStsData;
class CPressData;
class CLevelData;
class CPvData;
class CBankAlarm;
typedef struct _SAlarm SAlarm;
typedef struct _SAlarmParam SAlarmParam;
class QSharedMemory;

//实时共享内存数据库(RealTimeDataBase)
class CRTDB : public QObject
{
	Q_OBJECT
public:
    CRTDB(QString keyName = "KGDZ_RTDB_BAMS", QObject *parent = 0);
	~CRTDB();

public:
    //创建内存数据库:true创建成功,false创建失败
    bool CreateRtdb();
    //附着打开内存数据库:true加载成功,false加载失败
    bool AttachRtdb();
    //获取共享内存的总大小,单位为byte
    int GetSize();

    //获取电池堆 (iBattStackIndex>=0 && iBattStackIndex<1)
	CBattStack* getStack(int iBattStackIndex);
    CBattStack* getStack0();
    //获取电池簇/组 (iBattBankIndex>=0 && iBattStackIndex<MAX_BANK_NUM)
	CBattBank* getBank(int iBattBankIndex);
    CBattBank* getBank0();
    CBattBank* getBank1();
    CBattBank* getBank2();
    CBattBank* getBank3();
    //获取电池寄存器 (iBattRegIndex>=0 && iBattStackIndex<MAX_REG_NUM)
    CBAComReg* getBAComReg(int iBattRegIndex);
    CBAComReg* getBAComReg0();
    CBAComReg* getBAComReg1();
    CBAComReg* getBAComReg2();
    CBAComReg* getBAComReg3();
    //获取PCS逆变器设备 (iPcsDevIndex>=0 && iPcsDevIndex<MAX_PCS_NUM)
    CPcsData* getPcs(int iPcsDevIndex);
    CPcsData* getPcs0();
    CPcsData* getPcs1();
    CPcsData* getPcs2();
    CPcsData* getPcs3();
    //获取电表设备 (iMeterDevIndex>=0 && iMeterDevIndex<MAX_METER_NUM)
    CMeterData* getMeter(int iMeterDevIndex);
    CMeterData* getMeter0();
    CMeterData* getMeter1();
    CMeterData* getMeter2();
    CMeterData* getMeter3();
    //获取消防设备 (iFireDevIndex>=0 && iFireDevIndex<MAX_FIRE_NUM)
    CFireData* getFireDev(int iFireDevIndex);
    CFireData* getFireDev0();
    CFireData* getFireDev1();
    CFireData* getFireDev2();
    CFireData* getFireDev3();
    //获取空调设备 (iAcDevIndex>=0 && iAcDevIndex<MAX_AC_NUM)
	CAcData* getAcDev(int iAcDevIndex);
    CAcData* getAcDev0();
    CAcData* getAcDev1();
    CAcData* getAcDev2();
    CAcData* getAcDev3();
    //获取变压器设备 (iTransDevIndex>=0 && iTransDevIndex<2)
	CTransData* getTransDev(int iTransDevIndex);
    CTransData* getTransDev0();
    CTransData* getTransDev1();
    //获取研华IO
	CAdamIOData* getAdamIODev();
    //获取IO模块 Dev0为屏后IO,Dev1为扩展IO (iIoDevIndex>=0 && iIoDevIndex<2)
    CIoData* getIoDev(int iIoDevIndex);
    CIoData* getIoDev0();
    CIoData* getIoDev1();
    //获取液冷设备 (iLcDevIndex>=0 && iLcDevIndex<MAX_LC_NUM)
    CLcData* getLcDev(int iLcDevIndex);
    CLcData* getLcDev0();
    CLcData* getLcDev1();
    CLcData* getLcDev2();
    CLcData* getLcDev3();
    //获取环控设备 (iEcDevIndex>=0 && iEcDevIndex<MAX_EC_NUM)
    CEcData* getEcDev(int iEcDevIndex);
    CEcData* getEcDev0();
    CEcData* getEcDev1();
    CEcData* getEcDev2();
    CEcData* getEcDev3();
    //获取温湿度设备 (iThDevIndex>=0 && iThDevIndex<MAX_TH_NUM)
    CThData* getThDev(int iThDevIndex);
    CThData* getThDev0();
    CThData* getThDev1();
    CThData* getThDev2();
    CThData* getThDev3();
    //获取除湿设备 (iDhDevIndex>=0 && iDhDevIndex<MAX_DH_NUM)
    CDhData* getDhDev(int iDhDevIndex);
    CDhData* getDhDev0();
    CDhData* getDhDev1();
    CDhData* getDhDev2();
    CDhData* getDhDev3();
    //获取水浸设备 (iWaterDevIndex>=0 && iWaterDevIndex<MAX_WATER_NUM)
    CWaterData* getWaterDev(int iWaterDevIndex);
    CWaterData* getWaterDev0();
    CWaterData* getWaterDev1();
    CWaterData* getWaterDev2();
    CWaterData* getWaterDev3();
    //获取门禁设备 (iDoorDevIndex>=0 && iDoorDevIndex<MAX_DOOR_NUM)
    CDoorData* getDoorDev(int iDoorDevIndex);
    CDoorData* getDoorDev0();
    CDoorData* getDoorDev1();
    CDoorData* getDoorDev2();
    CDoorData* getDoorDev3();
    //获取DCDC设备 (iDcdcDevIndex>=0 && iDcdcDevIndex<MAX_DCDC_NUM)
    CDcdcData* getDcdcDev(int iDcdcDevIndex);
    CDcdcData* getDcdcDev0();
    CDcdcData* getDcdcDev1();
    CDcdcData* getDcdcDev2();
    CDcdcData* getDcdcDev3();
    //获取充电桩设备 (iCpDevIndex>=0 && iCpDevIndex<MAX_PILE_NUM)
    CCpData* getCpDev(int iCpDevIndex);
    CCpData* getCpDev0();
    CCpData* getCpDev1();
    CCpData* getCpDev2();
    CCpData* getCpDev3();
    //获取可燃气体设备 (iGasDevIndex>=0 && iGasDevIndex<MAX_GAS_NUM)
    CGasData* getGasDev(int iGasDevIndex);
    CGasData* getGasDev0();
    CGasData* getGasDev1();
    CGasData* getGasDev2();
    CGasData* getGasDev3();
    //获取UPS (iUpsDevIndex>=0 && iUpsDevIndex<MAX_UPS_NUM)
    CUpsData* getUpsDev(int iUpsDevIndex);
    CUpsData* getUpsDev0();
    CUpsData* getUpsDev1();
    CUpsData* getUpsDev2();
    CUpsData* getUpsDev3();
    //获取LED灯 (iLedDevIndex>=0 && iLedDevIndex<MAX_LED_NUM)
    CLedData* getLedDev(int iLedDevIndex);
    CLedData* getLedDev0();
    CLedData* getLedDev1();
    CLedData* getLedDev2();
    CLedData* getLedDev3();
    //获取风扇灯 (iFanDevIndex>=0 && iFanDevIndex<MAX_FAN_NUM)
    CFanData* getFanDev(int iFanDevIndex);
    CFanData* getFanDev0();
    CFanData* getFanDev1();
    CFanData* getFanDev2();
    CFanData* getFanDev3();
    //获取STS (iStsDevIndex>=0 && iStsDevIndex<MAX_STS_NUM)
    CStsData* getStsDev(int iStsDevIndex);
    CStsData* getStsDev0();
    CStsData* getStsDev1();
    CStsData* getStsDev2();
    CStsData* getStsDev3();
    //获取压力传感器 (iPressDevIndex>=0 && iPressDevIndex<MAX_PRESS_NUM)
    CPressData* getPressDev(int iPressDevIndex);
    CPressData* getPressDev0();
    CPressData* getPressDev1();
    CPressData* getPressDev2();
    CPressData* getPressDev3();
    //获取液位传感器 (iLevelDevIndex>=0 && iLevelDevIndex<MAX_LEVEL_NUM)
    CLevelData* getLevelDev(int iLevelDevIndex);
    CLevelData* getLevelDev0();
    CLevelData* getLevelDev1();
    CLevelData* getLevelDev2();
    CLevelData* getLevelDev3();
    //获取光伏 (iPvDevIndex>=0 && iPvDevIndex<MAX_PV_NUM)
    CPvData* getPvDev(int iPvDevIndex);
    CPvData* getPvDev0();
    CPvData* getPvDev1();
    CPvData* getPvDev2();
    CPvData* getPvDev3();
    //获取预留设备1 (iRes1DevIndex>=0 && iRes1DevIndex<MAX_RES1_NUM)
    CReserve1Data* getRes1Data(int iRes1DevIndex);
    //获取进程APP (iAppDevIndex>=0 && iAppDevIndex<MAX_APP_NUM)
    CAppData* getAppData(int iAppDevIndex);

	int getComInputRegValBA(int iBAIndex, quint16 usRegAddr, quint16& usVal);
	int getComHoldRegValBA(int iBAIndex, quint16 usRegAddr, quint16& usVal);

	// Alarm
    //获取电池组告警
	CBankAlarm* getBankAlarm(int iBattBankIndex);
    SAlarm* getFirstAlarm(int iBattBankIndex);
    SAlarmParam* getAlarmParam(int iBattBankIndex);

    SAlarm* getFirstAlarm(CBankAlarm* pBankAlarm);
    SAlarm* getAlarm(CBankAlarm* pBankAlarm, quint16 usNo);

	// DryWet
	void setDryWetCtrl(CBattBank* pBattBank, quint16 usDry1Ctrl, quint16 usDry2Ctrl, quint16 usDry3Ctrl, quint16 usDry4Ctrl, quint16 usWetCtrl, int iDry1CtrlNum, int iDry2CtrlNum, int iDry3CtrlNum, int iDry4CtrlNum, int iWetCtrlNum);
	void setDryCtrl1(CBattBank* pBattBank, quint16 usDry1Ctrl, quint16 usDry2Ctrl, int iDry1CtrlNum, int iDry2CtrlNum);
	void setDryCtrl2(CBattBank* pBattBank, quint16 usDry3Ctrl, quint16 usDry4Ctrl, int iDry3CtrlNum, int iDry4CtrlNum);
	void setWetCtrl(CBattBank* pBattBank, quint16 usWetCtrl, int iWetCtrlNum);

private:
    int initRTDB(bool bReadWrite = false);

signals:
	void addLog(int iLogType, const QString& strLog);

private:
	int m_iBattStackNum;    /*电池堆-转发模块数*/
    int m_iBattRegNum;      /*电池寄存器数*/
    int m_iBattBankNum;     /*电池组数*/
    int m_iBattAlarmNum;    /*电池告警数*/
    int m_iPcsDevNum;       /*PCS 数*/
    int m_iMeterDevNum;     /*电表数*/
    int m_iFireDevNum;      /*消防设备数*/
	int m_iAcDevNum;        /*空调设备数*/
	int m_iTransDevNum;     /*变压器设备数*/
	int m_iAdamIODevNum;    /*ADAM模块数*/
    int m_iIoDevNum;        /*屏后IO模块数*/
    int m_iLcDevNum;        /*液冷机数*/
    int m_iEcDevNum;        /*环控设备数*/
    int m_iThDevNum;        /*温湿度设备数*/
    int m_iDhDevNum;        /*除湿设备数*/
    int m_iWaterDevNum;     /*水浸设备数*/
    int m_iDoorDevNum;      /*门禁设备数*/
    int m_iDcdcDevNum;      /*DCDC设备数*/
    int m_iCpDevNum;        /*充电桩设备数*/
    int m_iGasDevNum;       /*可燃气体数*/
    int m_iUpsDevNum;       /*UPS数*/
    int m_iLedDevNum;       /*LED灯数*/
    int m_iFanDevNum;       /*风扇数*/
    int m_iStsDevNum;       /*STS数*/
    int m_iPressDevNum;     /*压力传感器数*/
    int m_iLevelDevNum;     /*液位传感器数*/
    int m_iPvDevNum;        /*光伏数*/
    int m_iRes1DevNum;      /*预留设备1数*/
    int m_iAppDevNum;       /*进程APP数*/

    QSharedMemory*          m_pSharedMemery = nullptr;
    char*                   m_pRTDBData = nullptr;
    QHash<int, int>         m_hashType2Size; //key为内存类型,value为该内存类型的首地址大小
    int                     m_iSize = 0;
};

#endif // RTDB_H
